##This file produces Figure 1. Wrist-Measured Gunshot Example from Loeffler (2014), "Detecting Gunshots Using Wearable Accelerometers"

require(lattice)
require(latticeExtra)
require(extrafont)
require(grid)
font_import()
loadfonts(device = "postscript")

##Open training data file
feature<-read.csv("C:/Users/cloef/Box Sync/Gunshot Detection/Data/training_long_201.csv")
feature2 <- na.omit(feature)
feature2$time <- (feature2$count-26)*0.3
features <- feature2[order(feature2$reltime),]

#Subset data to single gunshot example
exemplar <- subset(features, window==459) 

##Open postscript file
postscript("C:/Users/cloef/Box Sync/Gunshot Detection/Data/figure1.eps",
	paper="special",height=3.27,width=6.83,family="Arial",horizontal=FALSE)
par(omi=c(0.0278,0.0278,0.0278,0.0278))

##Generate x-axis plot
a<-xyplot(exemplar$ax~exemplar$time,type="l",col="black",lty=1,lwd=0.5,ylim=range(c(-17,17)),xlim=range(c(-9.9,55)),
	xlab="ms",ylab="Gs",xlab.top="(a)",scales = list(col="black",tck = c(1, 0)),
	par.settings = list(axis.line=list(col = "transparent"), 
     	layout.heights=list(top.padding=-0.5, bottom.padding=-0.5,key.axis.padding=-0.75),
	layout.widths=list(left.padding=0.5,right.padding=-1.5,ylab.axis.padding = -0.5)),
	       axis = function(side, ...) {
           if (side == "left")
                grid.lines(x = c(0, 0), y = c(0, 1),
                           default.units = "npc")
           else if (side == "bottom")
                grid.lines(x = c(0, 1), y = c(0, 0),
                           default.units = "npc")
           axis.default(side = side, ...)
       })

##Generate y-axis plot
b<-xyplot(exemplar$ay~exemplar$time,type="l",col="black",lty=1,lwd=0.5,ylim=range(c(-17,17)),xlim=range(c(-9.9,55)),
	xlab="ms",ylab="Gs",xlab.top="(b)",scales = list(col="black",tck = c(1, 0)),
	par.settings=list(axis.line=list(col = "transparent"),layout.heights=list(top.padding=-0.5, bottom.padding=-0.5,key.axis.padding=-0.75),
	layout.widths=list(left.padding=0.5,right.padding=-1.5,ylab.axis.padding = -0.5)),
      axis = function(side, ...) {
           if (side == "left")
                grid.lines(x = c(0, 0), y = c(0, 1),
                           default.units = "npc")
           else if (side == "bottom")
                grid.lines(x = c(0, 1), y = c(0, 0),
                           default.units = "npc")
           axis.default(side = side, ...)
       })

##Generate z-axis plot
c<-xyplot(exemplar$az~exemplar$time,type="l",col="black",lty=1,lwd=0.5,ylim=range(c(-17,17)),xlim=range(c(-9.9,55)),
	xlab="ms",ylab="Gs",xlab.top="(c)",scales = list(col="black",tck = c(1, 0)),
	par.settings=list(axis.line=list(col = "transparent"),layout.heights=list(top.padding=-0.5, bottom.padding=-0.5,key.axis.padding=-0.75),
	layout.widths=list(left.padding=0.5,right.padding=-1.5,ylab.axis.padding = -0.5)),
      axis = function(side, ...) {
           if (side == "left")
                grid.lines(x = c(0, 0), y = c(0, 1),
                           default.units = "npc")
           else if (side == "bottom")
                grid.lines(x = c(0, 1), y = c(0, 0),
                           default.units = "npc")
           axis.default(side = side, ...)
       })

##Place plots side-by-side in postscript file
print(a, position = c(0, 0, 0.33, 1), more = TRUE)
print(b, position = c(0.33, 0, 0.66, 1), more = TRUE)
print(c, position = c(0.66,0,1,1))
dev.off()

